package com.icode.gulimall.ware.config;

import org.springframework.amqp.core.*;
import org.springframework.amqp.support.converter.Jackson2JsonMessageConverter;
import org.springframework.amqp.support.converter.MessageConverter;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * @Author: my_code
 * @Email: my_code@foxmail.com
 * @Create: 2021-02-06 13:28
 */
@Configuration
public class WareRabbitConfig {

    @Bean
    public MessageConverter messageConverter(){
        return new Jackson2JsonMessageConverter();
    }

    @Bean
    public Exchange stockEventExchange(){
        return ExchangeBuilder.topicExchange("stock-event-exchange").build();
    }

    @Bean
    public Queue lockedQueue(){
        return QueueBuilder.durable("stock.locked.queue").deadLetterRoutingKey("stock.release")
                .deadLetterExchange("stock-event-exchange").ttl(120000).build();
    }

    @Bean
    public Queue releaseQueue(){
        return QueueBuilder.durable("stock.release.queue").build();
    }

    @Bean
    public Binding releaseQueueBinding(){
        return BindingBuilder.bind(releaseQueue()).to(stockEventExchange())
                .with("stock.release").noargs();
    }

    @Bean
    public Binding delayQueueBinding(){
        return BindingBuilder.bind(lockedQueue()).to(stockEventExchange())
                .with("stock.locked").noargs();
    }
}
